package com.realtyManage.springboot.Controller;

import com.alibaba.fastjson.JSON;
import com.realtyManage.springboot.Config.AliMessage;
import com.realtyManage.springboot.Model.Do.Appointment;
import com.realtyManage.springboot.Model.Do.Area;
import com.realtyManage.springboot.Model.Do.User;
import com.realtyManage.springboot.Model.Vo.BaseEntity;
import com.realtyManage.springboot.Model.Vo.RegEntity;
import com.realtyManage.springboot.Service.AppointmentService;
import com.realtyManage.springboot.Service.AreaService;
import com.realtyManage.springboot.Service.IUserService;
import com.realtyManage.springboot.Utils.SysExecRsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/appointment")
public class AppointmentController {

    @Autowired
    private AppointmentService appointmentService;

    @Autowired
    private IUserService userService;

    /**
     * 查询预约信息
     * @param entity
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/showAppointmentList",method = RequestMethod.GET)
    private String showAppointmentList(BaseEntity entity, HttpServletRequest request) {
        Page<Appointment> appointmentsList = appointmentService.findPagedByWhereClause(entity);
        return JSON.toJSONString(appointmentsList);
    }


    /**
     * 物理删除城市信息
     * @param request
     * @return
     */
    @RequestMapping(value = "/deleteAppointment",method = RequestMethod.POST)
    @ResponseBody
    private String deleteAppointment(String id, HttpServletRequest request) {
        Integer code = -1;
        try{
            if (id != null || id != ""){
                if (appointmentService.deleteAppointment(id) > 0){
                    code = 1;
                }else{
                    code = -1;
                }
            }
        }catch (Exception e){
            e.getMessage();
        }
        RegEntity regInfo = new RegEntity();
        regInfo.setInfoCode(code);
        regInfo.setInfoText(SysExecRsUtil.getMsgFromCode(code));
        return JSON.toJSONString(regInfo);
    }


    /**
     * 新增预约来访
     * @param appointment
     * @param request
     * @return
     */
    @RequestMapping(value = "/addAppointment",method = RequestMethod.POST)
    @ResponseBody
    private String addAppointment(Appointment appointment, HttpServletRequest request) {
        Integer code = -1;
        appointment.setAddTime(new Date().toString());
        if (appointmentService.save(appointment).getId() != null){
            //通知经理主管预约信息
            List<User> userList = userService.findNotice();
            if (userList.size() > 0){
                for (int i = 0; i<userList.size();i++){
                    String mgPhone =userList.get(i).getTelephone();
                    String rs = AliMessage.sendMessageS(2,appointment.getUserName() ,appointment.getPhone(),appointment.getNum(),appointment.getComeTime(),mgPhone);
                    if (rs != null && rs.equals("OK")) {
                        System.out.println("短信发送成功");
                    } else {
                        System.out.println("短信发送失败");
                    }
                }
            }
            code = 1;
        }
        RegEntity regInfo = new RegEntity();
        regInfo.setInfoCode(code);
        regInfo.setInfoText(SysExecRsUtil.getMsgFromCode(code));
        return JSON.toJSONString(regInfo);
    }



}
